package com.gaogzhen.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.SysUser;
import com.gaogzhen.service.SysUserService;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @version 1.0
 * @description 员工控制器
 * @date 2022-05-27 11:09
 */
@RestController
@RequestMapping("/users")
@Api(tags = "员工管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation(value = "按条件分页查询员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "分页参数-当前页码"),
            @ApiImplicitParam(name = "size", value = "分页参数-每页条目"),
            @ApiImplicitParam(name = "mobile", value = "手机号"),
            @ApiImplicitParam(name = "fullname", value = "姓名")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> findByPage(@ApiIgnore Page<SysUser> page, String mobile, String fullname) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> sysUserPage = sysUserService.findByPage(page, mobile, fullname);
        return R.ok(sysUserPage);
    }

    @PostMapping
    @ApiOperation(value = "新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser", value = "员工信息")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R add(@RequestBody SysUser sysUser) {
        boolean s = sysUserService.add(sysUser);

        return s ? R.ok() : R.fail("新增失败");
    }

    @PutMapping
    @ApiOperation(value = "修改员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser", value = "员工信息")
    })
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R update(@RequestBody SysUser sysUser) {
        boolean s = sysUserService.updateUser(sysUser);
        return s ? R.ok() : R.fail("新增失败");
    }

    @DeleteMapping
    @ApiOperation(value = "批量删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "员工id数组")
    })
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R update(@RequestBody Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return R.ok();
        }
        boolean s = sysUserService.deleteUsersByIds(ids);
        return s ? R.ok() : R.fail("新增失败");
    }
}
